package com.lanou3g.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou3g.bean.Person;
import com.lanou3g.mapper.PersonMapper;
import com.lanou3g.service.PersonService;
import com.lanou3g.util.Result;
@Service
public class PersonServiceImpl implements PersonService {

	@Autowired
	private PersonMapper personMapper;
	
	@Override
	public Result getAllPersons(Integer page, Integer limit,Person person) {
		List<Person> personList = personMapper.getAllPersons(person);
		PageHelper.startPage(page, limit);
		PageInfo<Person> pageInfo = new PageInfo<>(personList);
		Result result = new Result(0,"获取数据成功!");
		result.setCount(pageInfo.getTotal());
		result.setData(pageInfo.getList());
		return result;
	}

	@Override
	public Result checkPersonByName(String name) {
		Person person = personMapper.checkPersonByName(name);
		if(person == null) {
			//如果没有查到,说明不存在这个人
			return new Result(0,"没有查到姓名是"+name+"的人!");
		}else {
			return new Result(1,name+"已经存在!");
		}
	}

	@Override
	public Result insertPerson(Person person) {
		int rows = personMapper.insertPerson(person);
		if(rows == 0) {
			//说明没有插入进去
			return new Result(1,"添加人员失败!");
		}else {
			return new Result(0,"添加成功!");
		}
	}

	@Override
	public Person getPersonById(Integer id) {
		return personMapper.getPersonById(id);
	}

	@Override
	public Result updatePerson(Person person) {
		int rows = personMapper.updatePerson(person);
		if(rows == 0) {
			//说明没有更新成功
			return new Result(1,"更新失败!");
		}else {
			return new Result(0,"更新成功!");
		}
	}

	@Override
	public Result deletePersonById(Integer id) {
		int rows = personMapper.deletePersonById(id);
		if(rows == 0) {
			//删除失败
			return new Result(1,"删除失败!");
		}else {
			return new Result(0,"删除成功!");
		}
	}

	@Override
	public List<Person> getPersonsByFlatsId(Integer id) {
		return personMapper.getPersonsByFlatsId(id);
	}

}
